package com.tj.jinzao.activity;

import android.app.Activity;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.tj.jinzao.R;
import com.tj.jinzao.base.BaseActivity;
import com.tj.jinzao.base.IBaseView;
import com.tj.jinzao.model.DetectQuestion;
import com.tj.jinzao.presenter.DetectPresenter;
import com.tj.jinzao.utils.ToastUtils;
import com.tj.jinzao.widgets.ChooseConfigDialog;

import java.util.List;

import butterknife.BindView;
import butterknife.BindViews;
import butterknife.OnClick;

/**
 * 检测回答问题界面
 * Created by wjy on 2019/1/6.
 */
public class DetectChooseActivity extends BaseActivity implements IBaseView<List<DetectQuestion>> {

    @BindViews({R.id.ll_choose_1, R.id.ll_choose_2, R.id.ll_choose_3, R.id.ll_choose_4, R.id.ll_choose_5
            , R.id.ll_choose_6, R.id.ll_choose_7, R.id.ll_choose_8, R.id.ll_choose_9, R.id.ll_choose_10})
    List<View> mAnswerLayouts;
    @BindViews({R.id.iv_choose_1, R.id.iv_choose_2, R.id.iv_choose_3, R.id.iv_choose_4, R.id.iv_choose_5
            , R.id.iv_choose_6, R.id.iv_choose_7, R.id.iv_choose_8, R.id.iv_choose_9, R.id.iv_choose_10})
    List<ImageView> mAnswerSelectViews;
    @BindViews({R.id.tv_choose_1, R.id.tv_choose_2, R.id.tv_choose_3, R.id.tv_choose_4, R.id.tv_choose_5
            , R.id.tv_choose_6, R.id.tv_choose_7, R.id.tv_choose_8, R.id.tv_choose_9, R.id.tv_choose_10})
    List<TextView> mAnswerViews;
    @BindView(R.id.btn_next)
    Button mNextView;
    @BindView(R.id.tv_title)
    TextView mTitleView;
    @BindView(R.id.tv_answer_index)
    TextView mIndexView;

    private DetectPresenter mPresenter;

    private List<DetectQuestion> mQuestions;

    private int mCurrentIndex;

    @Override
    protected void setupView() {
        super.setupView();
        mPresenter = new DetectPresenter(this);
        mPresenter.getQuestionList();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.detachView();
    }

    @Override
    protected int getLeftButtonResId() {
        return R.mipmap.ic_back;
    }
    ;
    @OnClick({R.id.ll_choose_1, R.id.ll_choose_2, R.id.ll_choose_3, R.id.ll_choose_4, R.id.ll_choose_5
            , R.id.ll_choose_6, R.id.ll_choose_7, R.id.ll_choose_8, R.id.ll_choose_9, R.id.ll_choose_10})
    public void select(View view) {
        int index = 0;
        for (View v : mAnswerLayouts) {
            if (v == view) {
                if(mCurrentIndex>=mQuestions.size()){
                    ToastUtils.showToast(DetectChooseActivity.this,"请稍后确认");
                    return;
                }
                DetectQuestion question = mQuestions.get(mCurrentIndex);
                if(!question.isMultiple()){
                    int index1 = 0;
                    for(ImageView imageView:mAnswerSelectViews){
                        imageView.setSelected(false);
                        if(index1 <question.getSelect().length){
                            question.getSelect()[index1] = false;
                            index1++;
                        }
                    }
                }
                boolean isSelect = question.getSelect()[index];
                mAnswerSelectViews.get(index).setSelected(!isSelect);
                question.getSelect()[index] = !isSelect;
                break;
            }
            index++;
        }
    }

    @Override
    protected void clickLeftButton() {
        if(mCurrentIndex==0){
            dialog("退出，检测流程全部终止");
        }else {
            mCurrentIndex--;
            switchQuestion();
        }

    }

    @Override
    public int getLayoutResource() {
        return R.layout.activity_choose_question;
    }

    @Override
    public Activity getContext() {
        return this;
    }

    @Override
    public void onRequestSuccess(List<DetectQuestion> detectQuestions) {
        mQuestions = detectQuestions;
        if (detectQuestions != null) {
            mCurrentIndex = 0;
            switchQuestion();
        }
    }

    @OnClick(R.id.btn_next)
    public void next() {
        if (!checkAnswerOk()) {
            ToastUtils.showToast(this, "请答题");
            return;
        }
        mCurrentIndex++;
        if (mCurrentIndex == mQuestions.size() - 1) {
            mNextView.setText("提交");
        } else if (mCurrentIndex == mQuestions.size()) {
            mPresenter.dealChoose(mQuestions);
            startActivity(new Intent(this, FaceDetectingActivity.class));
            finish();
            return;
        }
        switchQuestion();
    }

    private boolean checkAnswerOk() {
        DetectQuestion question = mQuestions.get(mCurrentIndex);
        for (boolean select : question.getSelect()) {
            if (select) return true;
        }
        return false;
    }

    public void switchQuestion() {
        DetectQuestion question = mQuestions.get(mCurrentIndex);
        List<String> label = question.getOptions();
        int index = 0;
        mTitleView.setText(question.getLabel());
        int size = label.size();
        for (int i = size; i < mAnswerLayouts.size(); i++) {
            mAnswerLayouts.get(i).setVisibility(View.GONE);
        }
        for (String s : label) {
            mAnswerLayouts.get(index).setVisibility(View.VISIBLE);
            mAnswerViews.get(index).setText(s);
            mAnswerSelectViews.get(index).setSelected(false);
            if (question.getSelect() == null) {
                question.setSelect();
            }
            index++;
        }
        mIndexView.setText((mCurrentIndex + 1) + "/" + mQuestions.size());
    }

    @Override
    public void onRequestNoData(String message) {

    }

    @Override
    public void onRequestError(String error_message) {

    }
    private ChooseConfigDialog mDialog;
    private void dialog(String content){
        mDialog = new ChooseConfigDialog(DetectChooseActivity.this, content);
        mDialog.show();
        mDialog.setOnClickLinstener(new ChooseConfigDialog.OnClickLinstener() {
            @Override
            public void onConfig(String mContent) {
                mDialog.dismiss();
                if(mContent.equals("1")){
                    finish();
                }

            }
        });
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if(mDialog!=null&&mDialog.isShowing()){
                return true;
            }
            if(mCurrentIndex==0){
                dialog("退出，检测流程全部终止");
            }else {
                mCurrentIndex--;
                switchQuestion();
            }

        }
        return true;

    }

}
